using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace WindowsGame
{
    /// <summary>
    /// This is a game component that implements IUpdateable.
    /// </summary>
    public class Camera2DComponent : Microsoft.Xna.Framework.GameComponent
    {

        private Vector2 cameraPosition;
        private float cameraZoom;
        private Matrix cameraMatrix;

        public Camera2DComponent(Game game)
            : base(game)
        {
            // TODO: Construct any child components here
        }

        /// <summary>
        /// Allows the game component to perform any initialization it needs to before starting
        /// to run.  This is where it can query for any required services and load content.
        /// </summary>
        public override void Initialize()
        {
            cameraPosition.X = 0;
            cameraPosition.Y = 0;

            cameraZoom = 1;

            base.Initialize();
        }

        /// <summary>
        /// Allows the game component to update itself.
        /// </summary>
        /// <param name="gameTime">Provides a snapshot of timing values.</param>
        public void Update(GameTime gameTime, int screenWidth, int screenHeight)
        {
            //Calculate matrix
            //Flip values on camera to keep sanity on reasonable levels. Without flipped values, the camera will be inverted.
            //Round by casting to avoid graphical issues
            cameraMatrix = Matrix.CreateTranslation(new Vector3((int)-cameraPosition.X, (int)-cameraPosition.Y, 0)) *
                                                    Matrix.CreateScale(new Vector3(cameraZoom, cameraZoom, 0));
                                                    //Matrix.CreateTranslation(new Vector3(screenWidth * 0.5f, screenHeight * 0.5f, 0));

            base.Update(gameTime);
        }

        //Getters and setters for camera's position.
        public Vector2 getPosition() { return this.cameraPosition; }
        public void setPosition(Vector2 value) { this.cameraPosition = value; }

        //Getters and setters for camera's zoom
        public float getZoom() { return this.cameraZoom; }
        public void setZoom(float value) { this.cameraZoom = value; }

        //Getter for camera matrix
        public Matrix getMatrix() { return cameraMatrix; }
    }
}
